<?php

class Application_Model_UserAddressMapper
{
	protected $_dbTable;
	
	public function setDbTable($dbTable)
	{
		if (is_string($dbTable)) {
			$dbTable = new $dbTable();
		}
		if (!$dbTable instanceof Zend_Db_Table_Abstract) {
			throw new Exception('Invalid table data gateway provided');
		}
		$this->_dbTable = $dbTable;
		return $this;
	}
	
	public function getDbTable()
	{
		if (null === $this->_dbTable) {
			$this->setDbTable('Application_Model_DbTable_UserAddress');
		}
		return $this->_dbTable;
	}
	
	public function save(Application_Model_UserAddress $address)
	{
		$data = array(
				'id' => $address->getId(),
				'userid' => $address->getUserId(),
				'mobile_tel' => $address->getMobileTel(),
				'email'   => $address->getEmail(),
				'home_tel' => $address->getHomeTel(),
				'name' => $address->getName(),
				'country' => $address->getCountry(),
				'province' => $address->getProvince(),
				'district' => $address->getDistrict(),
				'address' => $address->getAddress(),
				'zip_code' => $address->getZipCode(),
				'longititude' => $address->getLongitude(),
				'latitude' => $address->getLatitude(),
				//'created' => date('Y-m-d H:i:s'),
		);
	
		if (null === ($id = $address->getId())) {
			unset($data['id']);
			$this->getDbTable()->insert($data);
		} else {
			$this->getDbTable()->update($data, array('id = ?' => $id));
		}
	}
	
	public function find($id)
	{
		$address = new Application_Model_UserAddress();
		$result = $this->getDbTable()->find($id);
		if (0 == count($result)) {
			return;
		}
		$row = $result->current();
		$address->setId($row->id)
		->setUserId($row->userid )
		->setName($row->name)
		->setMobileTel($row->mobile_tel)
		->setHomeTel($row->home_tel)
		->setCountry($row->country)
		->setProvince($row->province)
		->setCity($row->city)
		->setDistrict($row->district)
		->setAddress($row->address)
		->setZipCode($row->zip_code)
		->setLongitude($row->longitude)
		->setLatitude($row->latitude)
		//->setEmail($row->email)
		//
		//->setPassword($row->password)
		//->setCreated($row->created)
		;
		return $address;
	}
	
	public function findByUserId($uid){
		$entries   = array();
		$table = $this->getDbTable();
		$db = $table->getAdapter();
		$where = $db->quoteInto('userid = ?', $uid);
		$resultSet = $table->fetchAll($where);
		
		foreach ($resultSet as $row) {
			$subscribe = new Application_Model_UserAddress();
			$subscribe->setId($row->id)
			->setUserId($row->userid )
			->setName($row->name)
			->setMobileTel($row->mobile_tel)
			->setHomeTel($row->home_tel)
			->setCountry($row->country)
			->setProvince($row->province)
			->setCity($row->city)
			->setDistrict($row->district)
			->setAddress($row->address)
			->setZipCode($row->zip_code)
			->setLongitude($row->longitude)
			->setLatitude($row->latitude)
			//->setPassword($row->password)
			//->setCreated($row->created);
			;
			$entries[] = $subscribe;
		}
		
		return $entries;
	}
	
	
	public function fetchAll()
	{
		$resultSet = $this->getDbTable()->fetchAll();
		$entries   = array();
		foreach ($resultSet as $row) {
			$entry = new Application_Model_UserAddress();
			$entry->setId($row->id)
			->setUserId($row->userid )
			->setName($row->name)
			->setMobileTel($row->mobile_tel)
			->setHomeTel($row->home_tel)
			->setCountry($row->country)
			->setProvince($row->province)
			->setCity($row->city)
			->setDistrict($row->district)
			->setAddress($row->address)
			->setZipCode($row->zip_code)
			->setLongitude($row->longitude)
			->setLatitude($row->latitude);
			$entries[] = $entry;
		}
		return $entries;
	}


}

